package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelVideoAdapter;
import flc.ast.databinding.ActivitySelVideoBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelVideoActivity extends BaseAc<ActivitySelVideoBinding> {

    public static int type;
    private SelVideoAdapter selVideoAdapter;
    private List<SelectMediaEntity> listSel = new ArrayList<>();//选择的集合pr
    private String selPath;
    private int oldPosition;


    @Override
    protected int onCreate() {
        return R.layout.activity_sel_video;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivSelVideoBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvProVideoStart.setOnClickListener(this);

        mDataBinding.rvSelVideoList.setLayoutManager(new GridLayoutManager(mContext, 3));
        selVideoAdapter = new SelVideoAdapter();
        mDataBinding.rvSelVideoList.setAdapter(selVideoAdapter);
        selVideoAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvProVideoStart) {  //开始投屏
            if (listSel.size() == 0 && selPath == null) {
                ToastUtils.showShort(R.string.please_sel_video);
                return;
            }
            if (type == 0) {
                ProVideoActivity.listPlay = listSel;
                startActivity(ProVideoActivity.class);
            } else if (type == 1) {
                LocalPlayActivity.sVideoPath = selPath;
                startActivity(LocalPlayActivity.class);
            }
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (type == 0) {
            if (selVideoAdapter.getItem(position).isChecked()) {
                selVideoAdapter.getItem(position).setChecked(false);
                listSel.remove(selVideoAdapter.getItem(position));
            } else {
                selVideoAdapter.getItem(position).setChecked(true);
                listSel.add(selVideoAdapter.getItem(position));
            }
            selVideoAdapter.notifyItemChanged(position);
        } else if (type == 1) {
            selVideoAdapter.getItem(oldPosition).setChecked(false);
            oldPosition = position;
            selVideoAdapter.getItem(position).setChecked(true);
            selPath = selVideoAdapter.getItem(position).getPath();
            selVideoAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void initData() {
        if (type == 0) {
            mDataBinding.tvProVideoStart.setText(getString(R.string.start_pro));
        } else if (type == 1) {
            mDataBinding.tvProVideoStart.setText(getString(R.string.play_text));
        }
        getPermission();
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO).reqPermissionDesc(getString(R.string.get_storage_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                getData();
            }
        }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    selVideoAdapter.setList(selectMediaEntities);
                    mDataBinding.rvSelVideoList.setVisibility(View.VISIBLE);
                    mDataBinding.ivSelVideoNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvSelVideoList.setVisibility(View.GONE);
                    mDataBinding.ivSelVideoNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}